/*
 * Copyright 2007-2010 Jeremy Brown.
 *
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @author <a href="http://jeremy-brown.com">Jeremy Brown</a>
 */

package groovy.prefuse.factory

import javax.swing.*

import prefuse.data.*
import prefuse.data.query.*
import prefuse.util.ui.*


public class UIFactory extends AbstractFactory
{
	private Class klass
	private Map injectMap = [:]

	public UIFactory()
	{
	}
	
	public UIFactory(Class klass)
	{
		this.klass = klass
	}
	
	public UIFactory(Class klass, Map map)
	{
		//This constructor allows injecting additional attributes, without user intervention.
		//It lets pfJRangeSliderV and pfJRangeSliderH specify the orientation: attribute
		this.klass = klass
		injectMap = map
	}
	
	boolean isLeaf()
	{
		return true
	}
    
    public Object newInstance(FactoryBuilderSupport builder, Object name, Object value, Map attributes) throws InstantiationException, IllegalAccessException
	{
		attributes.putAll(injectMap) //constructor may have provided additional attributes to be injected
		
		//Change preferredSize:[x,y] to preferredSize:new Dimension(x,y)
		if(attributes.preferredSize instanceof List && attributes.preferredSize.size() == 2)
		{
			attributes.preferredSize = new java.awt.Dimension(*attributes.preferredSize)
		}

		def buildClass
		if(klass)
		{
			buildClass = klass
		}
		else if(value instanceof Class)
		{
			buildClass = value
			value = null
		}
		
		if(value instanceof JComponent)
		{
			return value
		}
		
		
		def ui = null
		
		if(buildClass instanceof Class && JComponent.isAssignableFrom(buildClass))
		{
			if(JFastLabel.equals(buildClass))
			{
				if(value != null && value instanceof JFastLabel)
				{
					ui = value
				}
				else
				{
					def text = ""
					if(value != null){text = value}
					else if(attributes.text != null){text = attributes.remove("text")}
					
					ui = new JFastLabel(text)
				}
			}
			else if(JForcePanel.equals(buildClass))
			{
			}
			else if(JPrefuseTable.equals(buildClass))
			{
				def table = value
				if(table == null && attributes.table){table = attributes.remove("table")}
				ui = new JPrefuseTable(table)
			}
			else if(JPrefuseTree.equals(buildClass))
			{
			}
			else if(JRangeSlider.equals(buildClass))
			{
				if(value != null && value instanceof JRangeSlider)
				{
					ui = value
				}
				else
				{
					def queryBinding = null
					if(value != null && value instanceof RangeQueryBinding){queryBinding = value; value=null}
					else if(attributes.binding != null && attributes.binding instanceof RangeQueryBinding){queryBinding = attributes.remove("binding")}
					
					def orientation = attributes.remove("orientation")
					if(orientation == null){orientation = JRangeSlider.HORIZONTAL}

					def direction
					if(orientation == JRangeSlider.HORIZONTAL)
					{
						direction = attributes.remove("direction") ?: JRangeSlider.LEFTRIGHT_TOPBOTTOM
					}
					else
					{
						direction = attributes.remove("direction") ?: JRangeSlider.RIGHTLEFT_BOTTOMTOP
					}
					
					ui = queryBinding.createRangeSlider(orientation, direction)
				}

			}
			else if(JSearchPanel.equals(buildClass))
			{
				if(value != null && value instanceof JSearchPanel)
				{
					ui = value
				}
				else
				{
					def queryBinding = null
					if(value != null && value instanceof SearchQueryBinding){queryBinding = value; value=null}
					else if(attributes.binding != null && attributes.binding instanceof SearchQueryBinding){queryBinding = attributes.remove("binding")}
					
					def monitorKeys = true
					if(attributes.monitor != null){monitorKeys = attributes.remove("monitor")}
					else if(attributes.monitorKeys != null){monitorKeys = attributes.remove("monitorKeys")}
					else if(attributes.monitorKeystrokes != null){monitorKeys = attributes.remove("monitorKeystrokes")}
					
					ui = queryBinding.createSearchPanel(monitorKeys)
				}
			}
			else if(JToggleGroup.equals(buildClass))
			{
			}
			else if(JValueSlider.equals(buildClass))
			{
			}
		}
		
		return ui
    }
}
